unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,HisYlPayJK, StdCtrls,EncdDecd,MD5Func,uLkJSON;

type
  TFormMain = class(TForm)
    MemoIn: TMemo;
    MemoOut: TMemo;
    Button1: TButton;
    Editnoise: TEdit;
    EditFunc: TEdit;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FormMain: TFormMain;

implementation

{$R *.dfm}

procedure TFormMain.Button1Click(Sender: TObject);
const
  sAppid = 'apptest';
  sAppKey = 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALBzjnozawiA7qLsqkgyASSPGay';
  sVersion = '1.0';
var
  sInput,sSign,sNoise,sFunc,sOutput : string;
  JKSoap : HisYlPayJKSoap;
  js : TlkJSONbase;
begin
//
  MemoOut.Clear;
  MemoOut.Lines.Add('Appid:'+sAppid);
  MemoOut.Lines.Add('AppKey:'+sAppKey);
  sFunc := Trim(EditFunc.Text);
  MemoOut.Lines.Add('func:'+sFunc);
  sNoise := Trim(Editnoise.Text);
  MemoOut.Lines.Add('noise:'+sNoise);
  sInput := Trim(MemoIn.Text);
  MemoOut.Lines.Add('json:');
  MemoOut.Lines.Add(sInput);
  sInput := EncodeString(sInput);
  sInput := StringReplace(sInput,#$D#$A,'',[rfReplaceAll, rfIgnoreCase]);
  MemoOut.Lines.Add('base64:');
  MemoOut.Lines.Add(sInput);

  sSign := Format('appid=%s&data=%s&noise=%s',[sAppid,sInput,sNoise]);
  sSign := sSign + Format('&key=%s&version=%s',[sAppKey,sVersion]);
  sSign := LowerCase(MD5(sSign,True));
  MemoOut.Lines.Add('Sign:'+sSign);

  sInput := Format('{"appid":"%s","func":"%s","data":"%s","noise":"%s","version":"%s","sign":"%s"}',
    [sAppid,sFunc,sInput,sNoise,sVersion,sSign]);
  MemoOut.Lines.Add('Input:');
  MemoOut.Lines.Add(sInput);

  JKSoap := GetHisYlPayJKSoap();
  sOutput := JKSoap.DoHisAction(sInput);
  MemoOut.Lines.Add('Output:');
  MemoOut.Lines.Add(sOutput);

  js := TlkJSON.ParseText(sOutput);
  try
    if js.Field['data'] <> nil then
    begin
      sOutput := js.Field['data'].Value;
      if sOutput <> '' then
      begin
        sOutput := DecodeString(sOutput);
        sOutput := UTF8Decode(sOutput);
        MemoOut.Lines.Add('data:');
        MemoOut.Lines.Add(sOutput);
      end;
    end;
  finally
    FreeAndNil(js);
  end;

end;

end.
